package lib

import (
	"RMS/app/shop/model"
	"RMS/common"
	"RMS/common/db"
	"context"
	"gorm.io/gorm"
	"log"
)

func ImplTableList(ctx context.Context, req *model.TableListReq) *model.TableListResp {
	resp := &model.TableListResp{}

	d := db.NewRmsDBRequest(ctx)
	d = d.Table(db.PmsTable)

	tableListFilter(ctx, req, d)

	var total int64
	d.Count(&total)

	if req.PageSize != nil && req.PageIndex != nil {
		d.Offset((*req.PageIndex - 1) * *req.PageSize).Limit(*req.PageSize)
	}

	var data []map[string]interface{}
	if err := d.Find(&data).Error; err != nil && err != gorm.ErrRecordNotFound {
		log.Printf("ImplPackageList err := %v\n", err.Error())
		resp.Status = common.ErrCode
		resp.Message = common.FindErrMessage
		return resp
	}
	log.Println(data)

	resp.Status = common.SuccessCode
	resp.Message = common.SuccessMessage
	resp.Data = data
	resp.Total = int(total)
	return resp
}
func tableListFilter(ctx context.Context, req *model.TableListReq, d *gorm.DB) {
	if req.Status != nil {
		d.Where("status = ?", *req.Status)
	}
}
